package com.plum.admin.modules.social.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.plum.admin.common.util.SecurityUtil;
import com.plum.admin.config.security.SecurityUserDetails;
import com.plum.admin.modules.social.entity.SysSocial;
import com.plum.admin.modules.social.mapper.SysSocialMapper;
import com.plum.admin.modules.social.service.ISysSocialService;
import com.plum.admin.modules.sys.entity.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 社交登录表 服务实现类
 * </p>
 *
 * @author 子丶沫
 * @since 2021-09-16
 */
@Service
public class SysSocialServiceImpl extends ServiceImpl<SysSocialMapper, SysSocial> implements ISysSocialService {
    @Autowired
    private SecurityUtil securityUtil;
    @Value("${social.callbackFeRelateUrl}")
    private String callbackFeRelateUrl;

    @Value("${social.callbackFeUrl}")
    private String callbackFeUrl;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public SysSocial findSocialByOpenId(String openId, Integer platform) {
        return baseMapper.selectOne(Wrappers.<SysSocial>lambdaQuery()
                .eq(SysSocial::getOpenId,openId)
                .eq(SysSocial::getPlatform,platform)
        );
    }

    /**
     * 生成服务器重定向地址
     * @param social 调用第三方封装的social对象
     * @param sysSocial 通过openId和类型查询的social对象
     * @param type 社交类型
     * @return
     */
    @Override
    public String getOauthLoginUrl(SysSocial social, SysSocial sysSocial, Integer type) {
        String url;
        if(ObjectUtil.isNotNull(sysSocial)&& StrUtil.isNotBlank(sysSocial.getUserId())){
            String token = securityUtil.getToken(sysSocial.getUserName());
            String tokenKey = IdUtil.simpleUUID();
            redisTemplate.opsForValue().set(tokenKey,token,2L, TimeUnit.MINUTES);
            url=callbackFeUrl+"?related=1&tokenKey=" + tokenKey;
            UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
                    new SecurityUserDetails(new SysUser().setUsername(sysSocial.getUserName())), null, null);
            SecurityContextHolder.getContext().setAuthentication(authentication);
        }else{
            String idToken = IdUtil.simpleUUID();
            redisTemplate.opsForValue().set(idToken, String.valueOf(social.getId()), 5L, TimeUnit.MINUTES);
            url = callbackFeRelateUrl + "?socialType=" + type + "&id=" + idToken;
        }
        return url;
    }
}
